package jrain.fw.core.utils;

import jrain.fw.core.business.BusinessProvider;
import jrain.fw.core.business.client.BusinessApiClient;
import jrain.fw.core.business.client.BusinessInnerClient;
import jrain.fw.core.business.client.BusinessRpcClient;
import jrain.fw.core.cfg.CfgService;
import jrain.fw.core.dao.ColumnFormatProvider;
import jrain.fw.core.dao.DataSourceService;
import jrain.fw.core.dao.SqlParser;
import jrain.fw.core.dao.SqlRunnerManger;
import jrain.fw.core.dao.TransactionManager;
import jrain.fw.core.http.HttpClientService;
import jrain.fw.core.id.IdService;
import jrain.fw.core.json.JsonService;
import jrain.fw.core.service.ServiceConst;
import jrain.fw.core.service.ctx.CtxService;
import jrain.fw.core.service.ctx.CtxServiceProvider;
import jrain.utils.collection.RMap;
import jrain.utils.exception.ServiceException;

/**
 * <pre>
 * 作者：3244924214@qq.com 
 * 描述： 框架工具类
 * </pre>
 */
public class FwUtils {

	private static RMap<String, Object> fwCfg = null;

	public static void setFwCfg(RMap<String, Object> cfg) {
		if (fwCfg == null) {
			fwCfg = cfg;
		}
	}

	public static RMap<String, Object> getFwCfg() {
		if (fwCfg == null) {
			throw new ServiceException("Service configuration is not initialized!");
		}
		return fwCfg;
	}

	public static CtxService getServiceContext() {
		return CtxServiceProvider.getContext();
	}

	// -----------------------------------------------------------------------------------------------------
	public static BusinessInnerClient getBusinessInnerClient() {
		return getServiceContext().getService(ServiceConst.S_BUSINESS_INNER_CLIENT, BusinessInnerClient.class);
	}

	public static BusinessRpcClient getBusinessRpcClient() {
		return getServiceContext().getService(ServiceConst.S_BUSINESS_RPC_CLIENT, BusinessRpcClient.class);
	}

	public static BusinessApiClient getBusinessApiClient() {
		return getServiceContext().getService(ServiceConst.S_BUSINESS_API_CLIENT, BusinessApiClient.class);
	}

	public static BusinessProvider getBusinessProvider() {
		return getServiceContext().getService(ServiceConst.S_BUSINESS_PROVIDER, BusinessProvider.class);
	}

	// -----------------------------------------------------------------------------------------------------
	public static CfgService getCfgService() {
		return getServiceContext().getService(ServiceConst.S_CFG_SERVICE, CfgService.class);
	}

	public static IdService getIdService() {
		return getServiceContext().getService(ServiceConst.S_ID_SERVICE, IdService.class);
	}

	public static JsonService getJsonService() {
		return getServiceContext().getService(ServiceConst.S_JSON_SERVICE, JsonService.class);
	}

	// -----------------------------------------------------------------------------------------------------
	public static ColumnFormatProvider getColumnFormatProvider() {
		return getServiceContext().getService(ServiceConst.S_DAO_COLUMN_FORMAT_PROVIDER, ColumnFormatProvider.class);
	}

	public static SqlParser getSqlParser() {
		return getServiceContext().getService(ServiceConst.S_DAO_SQL_PARSER_SERVICE, SqlParser.class);
	}

	public static SqlRunnerManger getSqlRunnerManger() {
		return getServiceContext().getService(ServiceConst.S_DAO_SQL_RUNNER_MANAGER, SqlRunnerManger.class);
	}

	public static DataSourceService getDataSourceService() {
		return getServiceContext().getService(ServiceConst.S_DAO_DATASOURCE_SERVICE, DataSourceService.class);
	}

	public static TransactionManager getTransactionManager() {
		return getServiceContext().getService(ServiceConst.S_DAO_TRANSACTION_MANAGER, TransactionManager.class);
	}

	public static HttpClientService getHttpClientService() {
		return getServiceContext().getService(ServiceConst.S_HTTP_CLIENT_SERVICE, HttpClientService.class);
	}
}
